
# REQUIRES: Data/Framing the Exit Experiment Raw Data.csv

# NOTE: Uses code from Stargazer Package Updates.R instead of the published version of Stargazer. See readme.txt.

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Experiment/Cleaning.R")

################# Table S8 - Covariate Balance Tests #################
### Crosstabs
cooperate <- aov(cooperate ~ condition, data = data)
cooperate <- print(summary(cooperate), digits=2, format = "f")
cooperate <- data.frame(unclass(cooperate), check.names = FALSE)
cooperate <- head(cooperate, 1)
cooperate$tab <- "Cooperate with Different People"

trust <- aov(trust ~ condition, data = data)
trust <- print(summary(trust), digits=2, format = "f")
trust <- data.frame(unclass(trust), check.names = FALSE)
trust <- head(trust, 1)
trust$tab <- "Trust Different People"

military_force <- aov(military_force ~ condition, data = data)
military_force <- print(summary(military_force), digits=2, format = "f")
military_force <- data.frame(unclass(military_force), check.names = FALSE)
military_force <- head(military_force, 1)
military_force$tab <- "Opinion on Use of Military Force"

great_powers <- aov(great_powers ~ condition, data = data)
great_powers <- print(summary(great_powers), digits=2, format = "f")
great_powers <- data.frame(unclass(great_powers), check.names = FALSE)
great_powers <- head(great_powers, 1)
great_powers$tab <- "Opinion on Great Powers"

political_affiliation <- aov(political_affiliation ~ condition, data = data)
political_affiliation <- print(summary(political_affiliation), digits=3, format = "f")
political_affiliation <- data.frame(unclass(political_affiliation), check.names = FALSE)
political_affiliation <- head(political_affiliation, 1)
political_affiliation$tab <- "Political Affiliation"

culture_panel <- aov(culture_panel ~ condition, data = data)
culture_panel <- print(summary(culture_panel), digits=2, format = "f")
culture_panel <- data.frame(unclass(culture_panel), check.names = FALSE)
culture_panel <- head(culture_panel, 1)
culture_panel$tab <- "Views of Other Cultures"

religion <- aov(religion ~ condition, data = data)
religion <- print(summary(religion), digits=3, format = "f")
religion <- data.frame(unclass(religion), check.names = FALSE)
religion <- head(religion, 1)
religion$tab <- "Religion"

religious_importance <- aov(religious_importance ~ condition, data = data)
religious_importance <- print(summary(religious_importance), digits=3, format = "f")
religious_importance <- data.frame(unclass(religious_importance), check.names = FALSE)
religious_importance <- head(religious_importance, 1)
religious_importance$tab <- "Religious Importance"

gender <- aov(gender ~ condition, data = data)
gender <- print(summary(gender), digits=2, format = "f")
gender <- data.frame(unclass(gender), check.names = FALSE)
gender <- head(gender, 1)
gender$tab <- "Gender"

age <- aov(age ~ condition, data = data)
age <- print(summary(age), digits=2, format = "f")
age <- data.frame(unclass(age), check.names = FALSE)
age <- head(age, 1)
age$tab <- "Age"

race <- aov(race ~ condition, data = data)
race <- print(summary(race), digits=2, format = "f")
race <- data.frame(unclass(race), check.names = FALSE)
race <- head(race, 1)
race$tab <- "Race"

education <- aov(education ~ condition, data = data)
education <- print(summary(education), digits=3, format = "f")
education <- data.frame(unclass(education), check.names = FALSE)
education <- head(education, 1)
education$tab <- "Education Level"

income <- aov(income ~ condition, data = data)
income <- print(summary(income), digits=3, format = "f")
income <- data.frame(unclass(income), check.names = FALSE)
income <- head(income, 1)
income$tab <- "Income"

military <- aov(military ~ condition, data = data)
military <- print(summary(military), digits=2)
military <- data.frame(unclass(military), check.names = FALSE)
military <- head(military, 1)
military$tab <- "In the Military?"

crosstabs <- rbind(cooperate, trust, military_force, great_powers, political_affiliation, culture_panel,
                   religion, religious_importance, gender, age, race, education, income, military)

### Table S8
crosstabs <- crosstabs %>% dplyr::select(tab, "F value", "Pr(>F)")
names(crosstabs)[names(crosstabs) == 'tab'] <- ''

myt <- ttheme_default(
  # Use hjust and x to left justify the text
  # Alternate the row fill colours
  core = list(fg_params=list(hjust = 1, x=1),
              bg_params=list(fill=c("white"))),
  
  # Change column header to white text and red background
  colhead = list(fg_params=list(col="black"),
                 bg_params=list(fill="white"))
)

png("Plots/Table S8 - Covariate Balance Tests.png", height = 50*nrow(crosstabs), width = 200*ncol(crosstabs))
grid.table(crosstabs, theme=myt, rows = NULL)
dev.off()

rm(cooperate, trust, military_force, great_powers, political_affiliation, culture_panel,
   religion, religious_importance, gender, age, race, education, income, military)

rm(crosstabs, myt)
